package ntd.util.modular.ai.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.db.Entity;
import ntd.util.modular.ai.entity.Ai;
import ntd.util.modular.ai.mapper.AiMapper;
import ntd.util.modular.ai.service.AiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author: yyd
 * Date: 2023-10-24 14:27
 */
@Service
public class AiServiceImpl implements AiService {
    @Autowired
    AiMapper aiMapper;

    @Override
    public List<Ai> server() throws SQLException {
        List<Entity> list = aiMapper.findAll();
        return list.stream().map(entity -> entity.toBean(new Ai())).collect(Collectors.toList());
    }

    @Override
    public Object add(Ai ai) throws SQLException {
        String id = UUID.fastUUID().toString().replace("-", "");
        ai.setId(id);
        Entity entity = Entity.create("ai_server");
        entity.parseBean(ai, true, true);
        return aiMapper.add(entity);
    }

    @Override
    public Object edit(String id, Ai ai) throws SQLException {
        Entity where = Entity.create("ai_server").set("id", id);
        Entity record = Entity.create("ai_server");
        ai.setId(null);
        record.parseBean(ai, true, true);
        return aiMapper.update(record, where);
    }

    @Override
    public Object delete(String id) throws SQLException {
        return aiMapper.deleteById(id);
    }

    @Override
    public List<Ai> detail(String id) throws SQLException {
        List<Entity> list = aiMapper.selectById(id);
        return list.stream().map(entity -> entity.toBean(new Ai())).collect(Collectors.toList());
    }
}
